package com.apollow.oschina.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.apollow.oschina.R;
import com.apollow.oschina.base.BaseActivityVPAdapter;
import com.apollow.oschina.base.BaseVPActivity;
import com.apollow.oschina.base.Global;
/**
 * Created by Administrator on 2016/11/29.
 */

public class WebViewActivity extends BaseVPActivity {
    WebView mWbView;
    LinearLayout ll_loading;
    private WebSettings settings;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        setWebView();
    }
    private void initView() {
        setContentView(R.layout.activity_news_detail);
         mWbView= (WebView) findViewById(R.id.wb_view);
        ll_loading= (LinearLayout) findViewById(R.id.ll_loading);
    }

    @Override
    protected String setTitle(String text) {
        return "咨询详情";
    }
    @Override
    protected void setupTab(BaseActivityVPAdapter vpAdapter) {
    }

    public void setWebView() {
        String url = getIntent().getStringExtra("URL");
        mWbView.loadUrl(url);//webView加载网页需要的地址;
        settings = mWbView.getSettings();
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);//点击放大,缩小;
        settings.setJavaScriptEnabled(true);//获取动态缩小,放大;
        mWbView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);//开始加载,
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);//结束加载,但时这个方法不一定会被调用
            }
        });
        mWbView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress > 5 && ll_loading.getVisibility() == View.VISIBLE) {
                    Global.mHandler.postDelayed(
                            new Runnable() {
                                @Override
                                public void run() {
                                    ll_loading.setVisibility(View.GONE); //延迟的两种方式
                                }
                            }, 600
                    );
                }
            }
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });

    }
}
